﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml;
namespace LD.Host
{
    public class XMLSerializeHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string Serialize(object o)
        {
            return Serialize(o, new UTF8Encoding(true));
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="o"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string Serialize(object o, Encoding encode)
        {
            string result = string.Empty;
            //方法1：
            //using (MemoryStream output = new MemoryStream())
            //{
            //    XmlSerializer serializer = new XmlSerializer(o.GetType());
            //    serializer.Serialize(output, o);
            //    result = encode.GetString(output.ToArray());
            //}

            //方法2：
            using (MemoryStream output = new MemoryStream())
            {
                using (XmlTextWriter writer = new XmlTextWriter(output, encode))
                {
                    //XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                    //namespaces.Add("", "");                 
                    writer.Namespaces = true;
                    //writer.WriteStartDocument(true);
                    XmlSerializer serializer = new XmlSerializer(o.GetType());
                    serializer.Serialize(writer, o);
                }
                result = encode.GetString(output.ToArray());
            }
            //方法3：可删除xml声明和默认命名空间(xmlns:xsi,xmlns:xsd)
            //XmlWriterSettings setting = new XmlWriterSettings();
            //setting.Encoding = encode;
            //setting.OmitXmlDeclaration = true;
            //XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            //namespaces.Add("", "");
            //using (MemoryStream output = new MemoryStream())
            //{
            //    using (XmlWriter writer = XmlWriter.Create(output, setting))
            //    {
            //        XmlSerializer serializer = new XmlSerializer(o.GetType());
            //        serializer.Serialize(writer, o, namespaces);
            //        result = encode.GetString(output.ToArray());
            //    }
            //}
            result = result.Replace(@"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""", "");
            result = result.Replace(@"xmlns:xsd=""http://www.w3.org/2001/XMLSchema""", "");
            result = result.Remove(0, 1);

            //CheckXML(result);
            return result;
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string str) where T : class
        {
            return Deserialize<T>(str, new UTF8Encoding(false));
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string str, Encoding encode) where T : class
        {
            T result = null;
            //方法1：
            //using (MemoryStream input = new MemoryStream(encode.GetBytes(str)))
            //{
            //    XmlSerializer serializer = new XmlSerializer(typeof(T));
            //    result = serializer.Deserialize(input) as T;
            //}
            //方法2：
            using (MemoryStream input = new MemoryStream(encode.GetBytes(str)))
            {
                using (XmlTextReader reader = new XmlTextReader(input))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    result = serializer.Deserialize(reader) as T;
                }
            }
            //方法3：
            //using (MemoryStream input = new MemoryStream(encode.GetBytes(str)))
            //{
            //    using (XmlReader reader = XmlReader.Create(input))
            //    {
            //        XmlSerializer serializer = new XmlSerializer(typeof(T));
            //        result = serializer.Deserialize(reader) as T;
            //    }
            //}
            return result;
        }
    }
}
